'''ShelfNotebookPlugin

Provide the bottom "shelf" for running tests, rsa, etc. 

I really want to come up with a better name...

'''

import re
import wx
import wx.lib.agw.aui as aui
import wx.lib.agw.flatnotebook as fnb
import rwb
import sys
import firebrick
import os.path

FID = "rwb.core.shelf_notebook"

class ShelfNotebookPlugin(firebrick.Plugin):
    def __init__(self, app):
        super(ShelfNotebookPlugin, self).__init__(_id=FID,
                                                  required = True,
                                                  url=None,
                                                  version="0.1",
                                                  name = "Shelf Notebook",
                                                  default_settings = {})
        
    def enable(self):
        self.app.Register(ShelfNotebookPanel)
        self.active = True

    def disable(self):
        self.app.Unregister(ShelfNotebookPanel)
        self.active = False

class ShelfNotebookPanel(firebrick.BasePanel):
    name = "Shelf"
    _id = FID

    def __init__(self, window):
        super(ShelfNotebookPanel, self).__init__(window)
        self._notebook = fnb.FlatNotebook(self, wx.ID_ANY, 
                                          agwStyle=fnb.FNB_VC8|fnb.FNB_X_ON_TAB|fnb.FNB_BOTTOM)
        self.window = window
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self._notebook, 1, wx.EXPAND)
        self.SetSizerAndFit(sizer)
#        self._notebook.Show(False)

        window.auimgr.AddPane(self, aui.AuiPaneInfo().
                              Bottom().
                              CaptionVisible(True).
                              Caption("Service Panel").
                              Floatable(True).
                              Movable(False).
                              NotebookDockable(False).
                              Name(FID).
                              BestSize(wx.Size(600,200)))

        # tab1   = wx.Panel(self._notebook, wx.ID_ANY, size=(200,200))
        # tab2   = wx.Panel(self._notebook, wx.ID_ANY, size=(200,200))
        # st1 = wx.StaticText(tab1, wx.ID_ANY, "This is tab 1")
        # st2 = wx.StaticText(tab2, wx.ID_ANY, "This is tab 2")
        # self._notebook.AddPage(tab1, "Tab1")
        # self._notebook.AddPage(tab2, "Tab2")

    def IsShown(self, child_panel = None):
        if not super(ShelfNotebookPanel,self).IsShown():
            return False
        if child_panel is not None:
            idx = self._notebook.GetPageIndex(child_panel)
            if idx == -1:
                return False
        return True

    def ShowPanel(self, panel):
        '''Make sure the given panel is visible

        panel is a reference to a panel, *not* a panel id...
        '''
        idx = self._notebook.GetPageIndex(panel)
        if idx is None or idx == -1:
            # WTF? select=True causes the notebook tabs to be (re)drawn
            # in the wrong place. Using CallAfter seems to fix that.
            self._notebook.AddPage(panel, panel.name, select=False)
            idx = self._notebook.GetPageIndex(panel)
        wx.CallAfter(self._notebook.SetSelection, idx)
#        self.window.auimgr.Update()

        
    def AddPanel(self, wbid, select=True):
        '''Add a panel based on its wbid (eg: rwb.core.rsa)

        '''

        panel = self.app.GetPanel(self.window, wbid)
        self._notebook.AddPage(panel, panel.name, select=select)

